package kassiopeia.agent.seda.event;

import kassiopeia.agent.AgentStatusHolder;
import kassiopeia.agent.io.EventMessage;
import kassiopeia.agent.io.MessageReceiver;
import kassiopeia.agent.seda.SEDA;
import kassiopeia.commons.AgentStatus;
import kassiopeia.commons.io.Message;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("eventReceiver")
public class EventReceiver implements MessageReceiver {

    private static final Logger logger = Logger.getLogger(EventReceiver.class);

    @Autowired
    private SEDA seda;

    @Autowired
    private AgentStatusHolder statusHolder;

    @Override
    public void onMessage(Message message) {
        if(statusHolder.getStatus() == AgentStatus.TASK) {
            logger.debug("Event received from remote destination");
            EventMessage eventMessage = (EventMessage) message;
            seda.send(eventMessage.getInstanceId(), eventMessage.getOutputId(), eventMessage.getEvent());
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends Message>[] acceptedMessages() {
        return new Class[]{EventMessage.class};
    }

}
